<!--
 * @Author: tkp
 * @Date: 2024-12-26 16:47:03
-->
<template>
  <div class="login_contain">
    <div class="login_img">
      <MyElImage fit="cover" imgStyle="width: 100vw;height: 100vh" url="/fileoss/static/login_bg.png"></MyElImage>
    </div>
    <div class="right_contain">
      <div class="contain_body">
        <div class="body_title">
          登录权限管理系统
        </div>
        <div class="body_item">
          <Input v-model="queryForm.account" clearable placeholder="请输入账号"></Input>
        </div>
        <div class="body_item">
          <Input v-model="queryForm.password" type="password" placeholder="请输入密码" showPassword></Input>
        </div>
        <div class="body_item_code">
          <Input v-model="imgForm.verifyCode" placeholder="请输入验证码"></Input>
          <div class="item_code">
            <img class="code_img" @click="loadImg" :src="imgForm.img" />
            <div class="code_text" @click="loadImg">看不清? 换一张</div>
          </div>
        </div>
        <div class="body_item_btn">
          <div class="item_btn" @click="goHomeEmp()" v-if="loginFlag==0">用户登录</div>
          <div class="item_btn" @click="goHomeAdmin()" v-if="loginFlag==1">管理员登录</div>
        </div>
        <div class="body_item_flag" v-if="loginFlag==0" @click="changeRole(1)">
          <div>我是管理员</div>
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="body_item_flag" v-if="loginFlag==1" @click="changeRole(0)">
          <div>我是用户</div>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
    </div>
    <!-- <div class="pic_pop" v-if="picPop">
      <SlideVerify :loginFlag="loginFlag" @onSuccess="onSuccessFun"></SlideVerify>
    </div> -->
  </div>
</template>

<script>
import SlideVerify from './temp/slideVerify.vue'
import { empLoginPwd, authorityMenuBySysId, adminLoginPwd, getVerifyImage2, getVerifyImageAdmin2 } from '@/utils/api'
import { getMenu, filterAsyncPermission } from '@/utils/utils'
import { initDynamicRoutes } from '@/router/index'
export default {
  name: 'login',
  components: {
    SlideVerify,
  },
  data() {
    return {
      picPop: false,
      queryForm: {
        account: '',
        password: '',
      },
      imgForm: {
        img: '',
        uniqueId: '',
        verifyCode: '',
      },
      loginFlag: 0,
    }
  },
  mounted() {
    this.loadImg()
  },
  methods: {
    changeRole(val) {
      this.loginFlag = val
      this.imgForm.verifyCode = ''
      this.loadImg()
    },
    loadImg() {
      if (this.loginFlag === 0) {
        getVerifyImage2().then(res => {
          this.imgForm.uniqueId = res.data.uniqueId
          this.imgForm.verifyCode = res.data.verifyCode
          this.imgForm.img = 'data:image/gif;base64,' + res.data.img
        })
      } else {
        getVerifyImageAdmin2().then(res => {
          this.imgForm.uniqueId = res.data.uniqueId
          this.imgForm.verifyCode = res.data.verifyCode
          this.imgForm.img = 'data:image/gif;base64,' + res.data.img
        })
      }
    },
    // getLoadPic(val) {
    //   this.loginFlag = val
    //   this.picPop = true
    // },
    // onSuccessFun(e) {
    //   if (e) {
    //     if (this.loginFlag === 0) {
    //       this.picPop = false
    //       this.goHomeEmp(e)
    //     } else {
    //       this.picPop = false
    //       this.goHomeAdmin(e)
    //     }
    //   } else {
    //     this.picPop = false
    //   }
    // },
    goHomeEmp(data) {
      let param = {
        account: this.queryForm.account,
        password: this.queryForm.password,
        // verifyImageModel: {
        //   uniqueId: data.uniqueId,
        //   xwidth: data.xwidth,
        //   yheight: data.yheight,
        // }
        verifyTextCodeModel: {
          uniqueId: this.imgForm.uniqueId,
          verifyCode: this.imgForm.verifyCode
        }
      }
      empLoginPwd(param).then(res => {
        this.messageSuccPop(res.msg)
        if (res.data.accountData) {
          this.$store.commit('user/SET_TOKEN', res.data.token)
          window.localStorage.setItem('authority.user', JSON.stringify(res.data.accountData || {}))
          this.getMenu(res.data.accountData)
        }
      })
    },
    goHomeAdmin(data) {
      let param = {
        accountData: {
          sysAdminAccount: this.queryForm.account,
          password: this.queryForm.password,
        },
        // verifyImageModel: {
        //   uniqueId: data.uniqueId,
        //   xwidth: data.xwidth,
        //   yheight: data.yheight,
        // }
        verifyTextCodeModel: {
          uniqueId: this.imgForm.uniqueId,
          verifyCode: this.imgForm.verifyCode
        }
      }
      adminLoginPwd(param).then(res => {
        this.messageSuccPop(res.msg)
        if (res.data.accountData) {
          this.$store.commit('user/SET_TOKEN', res.data.token)
          window.localStorage.setItem('authority.user', JSON.stringify(res.data.accountData || {}))
          this.getMenu(res.data.accountData)
        }
      })
    },
    getMenu(empData) {
      let param = {
        operatorId: empData.employeeId || empData.sysAdminId,
        systemId: 'authority'
      }
      authorityMenuBySysId(param).then(res => {
        let data = res.data || []
        let { navMenu } = getMenu(data)
        let permission = filterAsyncPermission(JSON.stringify(data))
        if (permission[0]) {
          window.localStorage.setItem('authority.navMenu', JSON.stringify(navMenu || []))
          window.localStorage.setItem('authority.permission', JSON.stringify(permission || []))
          initDynamicRoutes()
          this.$nextTick(() => {
            this.$router.replace(`/${permission[0].frontRoute}`)
          })
        } else {
          this.messageErrorPop('权限不足')
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.login_contain {
  position: relative;
  min-width: 1200px;
  .login_img {
    width: 100vw;
    height: 100vh;
    min-width: 1200px;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-position: right top;
    // background-image: url("../../assets/img/login_bg.png");
  }
  .right_contain {
    top: 0;
    bottom: 0;
    width: 40%;
    right: 0;
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .contain_body {
      position: relative;
      cursor: pointer;
      background: rgba(255, 255, 255, 0.7);
      padding: 70px 60px;
      border-radius: 30px;
      .body_title {
        text-align: center;
        font-weight: 500;
        font-size: 28px;
        margin-bottom: 15px;
      }
      .body_item {
        min-width: 350px;
        padding: 15px 10px;
        .item_btn {
          flex: 1;
          font-size: 16px;
          border-radius: 10px;
          padding: 17px 20px;
          color: #fff;
          text-align: center;
          background: #005eac;
        }
        .item_btn:hover {
          background: #014a86;
        }
      }
      .body_item_code {
        min-width: 350px;
        padding: 15px 10px;
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 20px;
        .item_code {
          display: flex;
          flex-direction: column;
          gap: 7px;
          .code_img {
            width: 150px;
            height: 35px;
          }
          .code_text {
            color: #9d9b9b;
            font-size: 12px;
          }
        }
      }
      .body_item_btn {
        margin-top: 20px;
        min-width: 350px;
        display: flex;
        flex-direction: row;
        gap: 20px;
        .item_btn {
          flex: 1;
          font-size: 16px;
          border-radius: 10px;
          padding: 17px 20px;
          color: #fff;
          text-align: center;
          background: #005eac;
        }
        .item_btn:hover {
          background: #014a86;
        }
      }
      .body_item_flag {
        position: absolute;
        right: 20px;
        bottom: 20px;
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 6px;
        color: #9d9b9b;
        div {
          font-size: 14px;
        }
        i {
          font-size: 14px;
        }
      }
    }
  }
}
.pic_pop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  bottom: 0;
  background-color: rgba(27, 27, 27, 0.5);
}
/deep/ .el-input__inner {
  height: 55px;
  line-height: 55px;
  font-size: inherit;
}
/deep/ .el-input--medium {
  font-size: inherit;
}
</style>
